package com.example.chcrm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.chcrm.entity.common.ServletUtils;
import com.example.chcrm.entity.employee.Department;
import com.example.chcrm.entity.employee.EmployeeVo;
import com.example.chcrm.entity.employee.EssEmployee;
import com.example.chcrm.entity.common.ResponseMsg;
import com.example.chcrm.service.EssEmployeeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Map;

/**
 * @Author 邹
 * @Date 2025/3/4 10:04
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
@CrossOrigin // 允许跨域访问
public class EssEmployeeController {
    @Autowired
    EssEmployeeService essEmployeeService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public ResponseMsg<Object> login(@RequestBody EssEmployee req) {
        ResponseMsg<Object> msg = essEmployeeService.login(req);
        return msg;
    }

    /**
     * 分页查询人员
     *
     * essEmployee
     * @return
     */
    @PostMapping("/pageList")
    public ResponseMsg<Object> pageList(@RequestBody EssEmployee essEmployee) {
       return essEmployeeService.pageList(essEmployee);
    }

    /**
     * 添加用户
     *
     * @param employee
     * @return
     */
    @PostMapping("/add")
    public ResponseMsg<Object> add(@RequestBody EssEmployee employee) {

        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return essEmployeeService.add(employee,empVo);
    }

    /**
     * 用户禁用/启用
     */
    @PostMapping("/disable")
    public ResponseMsg<Object> disable(@RequestBody EssEmployee essEmployee) {
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return  essEmployeeService.disable(essEmployee,empVo);
    }

    /**
     * 用户修改
     */
    @PostMapping("/update")
    public ResponseMsg<Object> update(@RequestBody EssEmployee essEmployee) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (!StringUtils.isNotBlank(essEmployee.getId())) {
            msg.fail("修改id不能为空");
            return msg;
        }
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return  essEmployeeService.update(essEmployee,empVo);
    }

    /**
     * 用户重置密码
     */
    @PostMapping("/reset")
    public ResponseMsg<Object> reset(@RequestBody EssEmployee essEmployee) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (!StringUtils.isNotBlank(essEmployee.getId())) {
            msg.fail("修改id不能为空");
            return msg;
        }
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return  essEmployeeService.reset(essEmployee,empVo);
    }

    /**
     * 用户详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public ResponseMsg<Object> detail(@PathVariable String id) {
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return essEmployeeService.detail(id);
    }

    /**
     * 用户详情
     *
     * @param
     * @return
     */
    @GetMapping("/userInfo")
    public ResponseMsg<Object> detail() {
//        HttpServletRequest request = ServletUtils.getRequest();
//        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        HttpServletRequest request = ServletUtils.getRequest();
        Map<String, String>  map = ServletUtils.getHeaders(request);
        String empVoHeader=null;
        if (map !=null) {
            empVoHeader = map.get("empvo");
        }
        EmployeeVo empVo = new EmployeeVo();
        if (StringUtils.isNotBlank(empVoHeader)) {
            // 1. URL安全BASE64解码
            String base64 = empVoHeader.replace('-', '+').replace('_', '/');
            byte[] decodedBytes = Base64.getDecoder().decode(base64);
            // 2. UTF-8解码
            String empNm = new String(decodedBytes, StandardCharsets.UTF_8);
            empVo.setEmpNm(empNm);
        }

//        String id = empVo.getId();
        return essEmployeeService.userInfo(empVo);
    }

    /***
     * 用户导出
     * @param employee
     * @return
     * **/
    @RequestMapping("/userExport")
    public ModelAndView userExport(EssEmployee employee) {
        //获取登录用户信息
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        ModelAndView modelAndView = essEmployeeService.userExport(employee, empVo);
        return modelAndView;

    }


    /**
     * 修改密码
     *
     * @param
     * @param
     * @param
     * @return
     */
    @PostMapping("/updatePassword")
//    public ResponseMsg<Object> updatePassword(String oldPwd, String newPwd) {
    public ResponseMsg<Object> updatePassword(@RequestBody EssEmployee essEmployee) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        String oldPwd = essEmployee.getOldPwd();
        String newPwd = essEmployee.getNewPwd();
        if (!StringUtils.isNotBlank(oldPwd)) {
            msg.fail("请输入原密码");
            return msg;
        }
        if (!StringUtils.isNotBlank(newPwd) || newPwd.length() < 6 || newPwd.length() > 18) {
            msg.fail("请输入6-18位新密码");
            return msg;
        }
        HttpServletRequest request = ServletUtils.getRequest();
        Map<String, String>  map = ServletUtils.getHeaders(request);
        String empVoHeader=null;
        if (map !=null) {
            empVoHeader = map.get("empvo");
        }
        EmployeeVo empVo = new EmployeeVo();
        if (StringUtils.isNotBlank(empVoHeader)) {
            // 1. URL安全BASE64解码
            String base64 = empVoHeader.replace('-', '+').replace('_', '/');
            byte[] decodedBytes = Base64.getDecoder().decode(base64);
            // 2. UTF-8解码
            String empNm = new String(decodedBytes, StandardCharsets.UTF_8);
            empVo.setEmpNm(empNm);
        }
        //操作业务逻辑
        return essEmployeeService.updatePassword(oldPwd, newPwd, empVo);
    }
}
